package cfdt.ldl.sys.service;


import cfdt.common.enums.CommonCodeEnum;
import cfdt.common.exception.CodeException;
import cfdt.common.vo.ObjectBaseResponse;
import cfdt.ldl.db.biz.UserInfoBiz;
import cfdt.ldl.core.entity.UserInfo;
import cfdt.ldl.sys.api.request.LoginSysRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginSysService {


    private static final Logger logger = LoggerFactory.getLogger("LoginSysService");

    @Autowired
    private UserInfoBiz userInfoBiz;

    public ObjectBaseResponse<UserInfo> login(LoginSysRequest loginSysRequest) throws CodeException {

        String userName = loginSysRequest.getUserName();
        // 查询用户，两个方案，选一个写法就行，这里只是举例子，查询数据库需要在memberServer里做
        // 1.lamba表达式
        UserInfo userInfo = userInfoBiz.getOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName, userName));
        // 2.查询对象
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_NAME", userName);
        userInfo = userInfoBiz.getOne(queryWrapper);

        // 用户不存在
        if (userInfo == null) {
            throw new CodeException(CommonCodeEnum.CODE_0003);
        }

        // TODO 比对密码、比对状态等


        // 成功响应
        ObjectBaseResponse<UserInfo> loginResponse = new ObjectBaseResponse<>();
        loginResponse.setData(userInfo);
        return loginResponse;
    }


}
